# -*- coding: utf-8 -*-
"""
@Time : 2024/10/17 22:06
@Author : ChenXiaoliang
@Email : middlegod@sina.com
@File : test.py
"""
# import json
#
# with open('weibo_user.json', 'r') as f:
#     content = json.load(f)
# print(content, type(content))

import urllib.parse
import requests
from bs4 import BeautifulSoup


def get_weibo_search():
    kw = urllib.parse.quote("python")
    for page in range(1, 3):
        headers = {"X-Requested-With": "XMLHttpRequest",
                   "Cookie": "SUB=_2A25KC1qhDeRhGedG6VUS9S7PzTiIHXVpadJprDV8PUJbkNB-LXekkW1NUXc-KzxF7bQR4JwgC8f42H81GoxDuEFB; SUBP=0033WrSXqPxfM725Ws9jqgMF55529P9D9WFmpDoV36wskXrwCOfoDO7o5JpX5KMhUgL.Fo2ReoM0SK50SoB2dJLoIERLxK.LBK-L1K-LxK.LBKBLB--LxKqLBo-LBoUki--4i-iFiK.pi--fiKLsiKLs;",
                   "Referer": f"https://s.weibo.com/realtime?q={kw}&rd=realtime&tw=realtime&Refer=pic_realtime"}
        url = f"https://s.weibo.com/realtime?q={kw}&rd=realtime&tw=realtime&page={page}"
        r = requests.get(url, headers=headers)
        soup = BeautifulSoup(r.text, "html.parser")
        results = soup.find_all('div', class_="content")
        for result in results:
            user_name = result.find('a', class_="name").get_text()
            print(user_name)
            user_url = result.find('a', class_="name").get('href')
            print(user_url)
            created_at = result.find('div', class_='from').get_text().strip().replace(' ', '').replace('\n', '')
            print(created_at)
            title = result.find('p', class_='txt').get_text().strip().replace(' ', '').replace('\n', '')
            print(title)


if __name__ == '__main__':
    get_weibo_search()
